﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LoungeWare
{
    public partial class Form1 : Form
    {
        static ImdbServices.Imdb imdb = new ImdbServices.Imdb();

        public Form1()
        {
            InitializeComponent();
            imdb.SearchResultsDownloaded +=
            new ImdbServices.Imdb.SearchResultsDownloadedEventHandler( SearchResultsDownloaded );
            imdb.MovieInfoDownloaded +=
                new ImdbServices.Imdb.MovieInfoDownloadedEventHandler( MovieInfoDownloadedEventHandler );
        }

        private void btn_search_Click( object sender, EventArgs e )
        {
            imdb.SearchMovieAsync( txt_title.Text, true, true );
            btn_search.Enabled = false;
        }

        private void MovieInfoDownloadedEventHandler( ImdbServices.Movie M )
        {
            lbl_title.Text = M.Title;
            lbl_year.Text = Convert.ToString(M.Year);
                lbl_genres.Text = "";
            foreach( string item in M.Genres )
            {
                lbl_genres.Text += item + ","; 
            }
            lbl_rating.Text = M.Rating.ToString();
            txt_plot.Text = M.Description;

            lbl_studio.Text = M.Studios.Count>0 ? M.Studios[0] : "" ;

            if( M.ImageURL != "" )
            {
                pictureBox1.Image =
                    Image.FromStream(
                    new System.IO.MemoryStream(
                        new System.Net.WebClient().DownloadData( M.ImageURL ) ) );
            }
        }

        private void SearchResultsDownloaded( ImdbServices.MoviesResultset M )
        {
            treeView1.Nodes.Clear();
            TreeNode TnP;
            if( M.PopularTitles.Count>0 )
            {
            TnP = new TreeNode("Popular Titles");           
            foreach( ImdbServices.Movie Movie in M.PopularTitles )             
            {                
                TnP.Nodes.Add( new TreeNode() 
                {
                    Text = Movie.Title,
                    Tag = Movie.ImdbID
                });
            }
            treeView1.Nodes.Add(TnP);   
            }

            if( M.PartialMatches.Count>0 )
            {
            TnP = new TreeNode("Partial Matches");
            foreach( ImdbServices.Movie Movie in M.PartialMatches )             
            {
                TnP.Nodes.Add( new TreeNode() 
                {
                    Text = Movie.Title,
                    Tag = Movie.ImdbID
                });
            } 
            treeView1.Nodes.Add(TnP);    
            }

            if( M.ExactMatches.Count>0 )
            {
            TnP = new TreeNode("Exact Matches");
            foreach( ImdbServices.Movie Movie in M.ExactMatches )             
            {
                
                TnP.Nodes.Add( new TreeNode() 
                {
                    Text = Movie.Title,
                    Tag = Movie.ImdbID
                });
                TnP.Expand();
            }
            treeView1.Nodes.Add(TnP);    
            }
            
            btn_search.Enabled = true;
        }

        private void treeView1_AfterSelect( object sender, TreeViewEventArgs e )
        {
            if( treeView1.SelectedNode != null )
            {
                imdb.GetMovieInfoAsync(Convert.ToInt64(e.Node.Tag),false);   
            }
        }
    }
}
